//// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
//package com.linqingying.gitcode.pullrequest.ui.review
//
//import com.intellij.collaboration.ui.codereview.review.CodeReviewSubmitViewModel
//import com.intellij.collaboration.util.SingleCoroutineLauncher
//import com.intellij.platform.util.coroutines.childScope
//import kotlinx.coroutines.CancellationException
//import kotlinx.coroutines.CoroutineScope
//import kotlinx.coroutines.flow.MutableStateFlow
//import kotlinx.coroutines.flow.StateFlow
//import kotlinx.coroutines.flow.asStateFlow
//import com.linqingying.gitcode.api.data.GCPullRequestReviewEvent
//import com.linqingying.gitcode.pullrequest.data.GCPullRequestPendingReview
//import com.linqingying.gitcode.pullrequest.data.provider.GCPRReviewDataProvider
//
//interface GCPRSubmitReviewViewModel : CodeReviewSubmitViewModel {
//  /**
//   * If the current user is the author of the pull request
//   */
//  val viewerIsAuthor: Boolean
//
//  /**
//   * If there's a currently pending review
//   */
//  val hasPendingReview: Boolean
//
//  /**
//   * Discard the current pending review
//   */
//  fun discard()
//
//  /**
//   * Submit the review with a specified event
//   */
//  fun submit(event: GCPullRequestReviewEvent)
//}
//
//internal class GCPRSubmitReviewViewModelImpl(parentCs: CoroutineScope,
//                                             private val dataProvider: GCPRReviewDataProvider,
//                                             override val viewerIsAuthor: Boolean,
//                                             private val pendingReview: GCPullRequestPendingReview?,
//                                             private val onDone: () -> Unit)
//  : GCPRSubmitReviewViewModel {
//  private val cs = parentCs.childScope()
//
//  private val taskLauncher = SingleCoroutineLauncher(cs)
//
//  override val hasPendingReview: Boolean = pendingReview != null
//
//  override val isBusy: StateFlow<Boolean> = taskLauncher.busy
//  private val _error = MutableStateFlow<Throwable?>(null)
//
//  override val error: StateFlow<Throwable?> = _error.asStateFlow()
//
//  override val draftCommentsCount: StateFlow<Int> = MutableStateFlow(pendingReview?.commentsCount ?: 0)
//
//  override val text: MutableStateFlow<String> = dataProvider.pendingReviewComment
//
//  override fun submit(event: GCPullRequestReviewEvent) {
//    val body = text.value
//    taskLauncher.launch {
//      try {
//        if (pendingReview != null) {
//          dataProvider.submitReview(pendingReview.id, event, body)
//        }
//        else {
//          dataProvider.createReview(event, body)
//        }
//      }
//      catch (ce: CancellationException) {
//        throw ce
//      }
//      catch (e: Exception) {
//        _error.value = e
//        return@launch
//      }
//      _error.value = null
//      text.value = ""
//      onDone()
//    }
//  }
//
//  override fun discard() {
//    if (pendingReview != null) {
//      taskLauncher.launch {
//        dataProvider.deleteReview(pendingReview.id)
//        onDone()
//      }
//    }
//  }
//
//  override fun cancel() {
//    onDone()
//  }
//}
